package com.hust.bridgecheck.serviceImpl.BridgeSensor;

import com.hust.bridgecheck.entity.*;
import com.hust.bridgecheck.repository.*;
import com.hust.bridgecheck.service.BridgeSensor.SensorService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class SensorServiceImpl implements SensorService {
    @Autowired
    private SensorRepository sensorRepository;
    @Autowired
    private SensorDataRepository sensorDataRepository;
    @Autowired
    private WarningDataRepository warningDataRepository;
    @Autowired
    private EarlyWarningRepository earlyWarningRepository;
    @Autowired
    private DailySDataRepository dailySDataRepository;
    @Override
    public Sensor addSensor(Sensor sensor) {
        return sensorRepository.save(sensor);
    }

    @Override
    public List<Sensor> getAllSensor() {
        return sensorRepository.findAll();
    }

    @Override
    public List<Sensor> getBridgeSenors(Integer bridgeID) {
        return sensorRepository.getByBridgeId(bridgeID);
    }

    @Override
    public Sensor updateSensor(Sensor sensor) {
        return sensorRepository.save(sensor);
    }

    @Override
    public void deleteSensor(Integer sensorID) {
        sensorRepository.deleteById(sensorID);
    }

    @Override
    public Sensor getSensorByID(Integer sensorID) {
        return sensorRepository.findById(sensorID).get();
    }

    @Override
    public List<SensorData> getSensorDataByParaId(String paraId, String startTime, String endTime) {
        //通过时间获取传感器数据
        return sensorDataRepository.getSensorDataByCollectTime(paraId,startTime,endTime);
    }

    @Override
    public List<WarningData> getWarningDataByParaId(String paraId) {
        return warningDataRepository.getWarningDataByParaId(paraId);
    }

    @Override
    public List<WarningData> getWarningDataByBridgeId(Integer bridgeId) {
        return warningDataRepository.getWarningDataByBridgeID(bridgeId);
    }

    @Override
    public List<EarlyWarning> getEarlyWarningByBridgeId(Integer bridgeId) {
        return earlyWarningRepository.getEarlyWarningByBridgeID(bridgeId, 0);
    }

    @Override
    public int deleteEarlyWarningById(Integer earlywarningid) {
        return earlyWarningRepository.deleteEarlyWarningByID(earlywarningid);
    }

    @Override
    public DailySensorData addDailySensorData(DailySensorData dailySensorData) {
        return dailySDataRepository.save(dailySensorData);
    }

    @Override
    public List<DailySensorData> getDailySensorData(Integer bridge, String startime, String endtime) {
        return dailySDataRepository.getAllBridgeData(bridge,startime,endtime);
    }

    @Override
    public List<DailySensorData> getSumDailySensorData(Integer bridge, String startime, String endtime) {
        return dailySDataRepository.getBridgeDataByGroupAnd(bridge,startime,endtime);
    }

    @Override
    public List<WarningData> getMultiDailySenosorData(List<Integer> dataID) {
        return warningDataRepository.findAllById(dataID);
    }

    @Override
    public WarningData addWarningData(WarningData warningData) {
        return warningDataRepository.save(warningData);
    }

    @Override
    public EarlyWarning getEarlyWarningByParaID(String paraID) {
        return earlyWarningRepository.getEarlyWarningByParaID(paraID,0);
    }

    @Override
    public EarlyWarning addEarlyWarning(EarlyWarning earlyWarning) {
        return earlyWarningRepository.save(earlyWarning);
    }


}
